package com.massa.caronafacil.activities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.massa.caronafacil.R;
import com.massa.caronafacil.TabsViewPagerApplication;
import com.massa.caronafacil.db.SQLConnector;
import com.massa.caronafacil.user.Session;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

	private EditText loginEditText;
	private EditText senhaEditText;
	private Button loginButton;

    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.login_layout);

        loginEditText = (EditText) findViewById(R.id.editTextLogin);
        senhaEditText = (EditText) findViewById(R.id.editTextSenha);
        loginButton = (Button) findViewById(R.id.buttonLogin);
        loginButton.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected final void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

	@Override
	public void onClick(View v) {
        if (v != null) {
            switch (v.getId()) {
            case R.id.buttonLogin:
                //TODO implementar persistencia de login (?)
                SQLConnector.getInstance();
                
                if(login() == true) {
                    final Intent intentTabs = new Intent(TabsViewPagerApplication.getAppContext(),
                    		TabsViewPagerFragmentActivity.class);
                    startActivity(intentTabs);

                    Toast toast = Toast.makeText(LoginActivity.this, "Logado com sucesso!", Toast.LENGTH_SHORT);
                    toast.show();
                } else {
                	Toast toast = Toast.makeText(LoginActivity.this, "Login ou senha invalidos!", Toast.LENGTH_SHORT);
                    toast.show();
                }

                break;
            }
        }
	}
	
	private boolean login() {
		Connection conn = SQLConnector.getInstance().getConn();

		try {
			PreparedStatement stmt = null;
			String sql = "SELECT nome FROM usuarios WHERE login=? AND senha=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, loginEditText.getText().toString());
			stmt.setString(2, senhaEditText.getText().toString());
			ResultSet reset = stmt.executeQuery();

			if(reset.next()) {
				Session mSession = Session.getInstance ();
				mSession.setLogin(loginEditText.getText().toString());
				mSession.setNome(sql); //TODO verificar se a string sql esta com o nome do usuario guardado

				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			Log.w("Error connection","" + e.getMessage());
		}
		
		return false;
	}
}
